/*************************************************************************
C *** This program is proprietary to ELSAG BAILEY and may only be used ***
C *** as authorized  in a license agreement controlling such use.      ***
C*************************************************************************
C
C  10  Name
C
C      MOV_VOLGODONSK - Move Object function
C
C  20  Purpose
C	This Module contains Function needed to Move Object
C
C  30  Description
C       Author        Date         Version    Reason
C       ----------   -----------   -------   ---------------------------------
C       M.Giacomelli 23 Feb 1999   1.0       
C		M.Tavella	  2 Oct	2001   -------   Porting to NT.
C
C  40  Calling Sequence/Execution Criteria
C
C  50  Common Usage
C       Common/Item                       Usage
C       -------------  -------------------------------------------------
C       None
C
C  60  Input/Output
C
C       File     Usage                  Description
C       ----     -----  ------------------------------------------------
C       None.           (directly)
C
C  70  Method
C
C***********************************************************************/
#define ALONE_MOVTWN
#include "Mov_Tianwan.h"


/*------------------------------------------------------------------------------
** Function:		GetDynCurrentPhase
**
** Description:		Get Current Phase from Dynamic Parameters
**
** Input:
**
** Output:			Current_Phase		- Current Phase number
**
** Return:			RETSTAT				- RT_NORMAL	If Ok
**										- RT_FAILED	If Error reading File
** Modificata per utilizzo su NT.
*/

RETSTAT GetDynCurrentPhase	( int	*Current_Phase )
{
  RETCOND		stat;
  dynamic_struc	dyn;
  FILEIDX		file;

  file.num = F_DYNP;

  *Current_Phase = -1;

  stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn));
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  *Current_Phase = dyn.current_phase;

  return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		SetDynCurrentPhase
**
** Description:		Set Current Phase in Dynamic Parameters
**
** Input:			Current_Phase	- Current Phase number
**
** Output:
**
** Return:			RETSTAT			- RT_NORMAL	If Ok
**									- RT_FAILED	If Error writing File
** Modificata per utilizzo su NT.
*/

RETSTAT SetDynCurrentPhase	( int	Current_Phase )
{
  RETCOND		stat;
  dynamic_struc	dyn;
  FILEIDX		file;

  file.num= F_DYNP;
  stat = FileRead(&file, 1, 1, &dyn, sizeof(dyn) );
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  dyn.current_phase = Current_Phase;
  stat = FileWrite( &file, 1, 1, &dyn, sizeof(dyn));
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		GetDynNextPhase
**
** Description:		Get Next Phase from Dynamic Parameters
**
** Input:
**
** Output:			Next_Phase		- Next Phase number
**
** Return:			RETSTAT			- RT_NORMAL	If Ok
**
**									- RT_FAILED	If Error reading File
** Modificata per utilizzo su NT.
*/

RETSTAT GetDynNextPhase		( int	*Next_Phase )
{
  RETCOND		stat;
  dynamic_struc	dyn;
  FILEIDX		file;

  file.num = F_DYNP;
  *Next_Phase = -1;

  stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn) );
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  *Next_Phase = dyn.next_phase;

  return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		SetDynNextPhase
**
** Description:		Set Next Phase in Dynamic Parameters
**
** Input:			Next_Phase		- Next Phase number
**
** Output:
**
** Return:			RETSTAT			- RT_NORMAL	If Ok
**									- RT_FAILED	If Error writing File
** Modificata per utilizzo su NT.
*/

RETSTAT SetDynNextPhase	( int	Next_Phase )
{
  RETCOND		stat;
  dynamic_struc	dyn;
  FILEIDX		file;

  file.num = F_DYNP;

  stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn) );
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  dyn.next_phase = Next_Phase;
  stat = FileWrite(&file, 1, 1, &dyn, sizeof(dyn) );
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		GetDynNextMove_a
**
** Description:		Get Next Move_a from Dynamic Parameters
**
** Input:
**
** Output:			Next_Move_a		- Next Move automatic
**
** Return:			RETSTAT			- RT_NORMAL	If Ok
**									- RT_FAILED	If Error reading File
** Modificata per utilizzo su NT.
*/

RETSTAT GetDynNextMove_a	( int	*Next_Move_a )
{
  RETCOND		stat;
  dynamic_struc	dyn;
  FILEIDX		file;

  *Next_Move_a = -1;
  file.num = F_DYNP;

  stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn) );
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  *Next_Move_a = dyn.next_move_a;

  return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		SetDynNextMove_a
**
** Description:		Set Next Move_a in Dynamic Parameters
**
** Input:			Next_Move_a		- Next Move automatic
**
** Output:
**
** Return:			RETSTAT			- RT_NORMAL	If Ok
**									- RT_FAILED	If Error writing File
** Modificata per utilizzo su NT.
*/

RETSTAT SetDynNextMove_a	( int	Next_Move_a )
{
  RETCOND		stat;
  dynamic_struc	dyn;
  FILEIDX		file;

  file.num = F_DYNP;

  stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn) );
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  dyn.next_move_a = Next_Move_a;
  stat = FileWrite( &file, 1, 1, &dyn, sizeof(dyn) );
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		GetDynCurrentMode
**
** Description:		Get Current Mode from Dynamic Parameters
**
** Input:
**
** Output:			Current Mode	- Current Mode
**
** Return:			RETSTAT			- RT_NORMAL	If Ok
**									- RT_FAILED	If Error reading File
** Modificata per utilizzo su NT.
*/

RETSTAT GetDynCurrentMode	( int	*CurrentMode )
{
	RETCOND				stat;
	dynamic_struc		dyn;
	FILEIDX				file;
	TAGIDX				tagIdx;
	TAGVALEX			tagVal;
	int					infiVal;

	file.num = F_DYNP;

	*CurrentMode = -1;

	stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn) );
	if ( stat != R_NORMAL )
		return (RT_FAILED);

	stat = GetTagIdx(TWN_OP_MODE, &tagIdx);
	if(stat != R_NORMAL)
		return(RT_FAILED);

	stat = TagRead(&tagIdx, &tagVal);
	if(stat != R_NORMAL)
		return(RT_FAILED);

	if(tagVal.value.val_float == (float)(0))
		infiVal = M_AUTOMATIC;
	else if(tagVal.value.val_float == (float)(1))
		infiVal = M_SEMIAUTO;
	else if(tagVal.value.val_float == (float)(2))
		infiVal = M_PROTECT;
	else if(tagVal.value.val_float == (float)(4))
		infiVal = M_EMERGEN;
	else if(tagVal.value.val_float == (float)(5))
		infiVal = M_MAN_SINGLE_CH;
	else if(tagVal.value.val_float == (float)(3))
		infiVal = M_MAN_WO_INT;
	else 
		return(RT_FAILED);

	//Do la precedenza al valore presente sulla tag//
	if(dyn.current_mode != (int)(infiVal)){
		dyn.current_mode = (int)(infiVal);
		stat = FileWrite(&file, 1, 1, &dyn, sizeof(dyn));
		if(stat != R_NORMAL)
			return(RT_FAILED);
	}

	*CurrentMode = dyn.current_mode;

	return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		SetDynCurrentMode
**
** Description:		Set Current Mode in Dynamic Parameters
**
** Input:			CurrentMode		- Current Mode
**
** Output:
**
** Return:			RETSTAT			- RT_NORMAL	If Ok
**									- RT_FAILED	If Error writing File
** Modificata per utilizzo su NT.
*/

RETSTAT SetDynCurrentMode	( int	CurrentMode )
{
	RETCOND				stat;
	RETSTAT				retSt;
	dynamic_struc		dyn;
	FILEIDX				file;
	TAGIDX				tagIdx;
	TAGVALEX			tagVal;
	float				temp;

	file.num = F_DYNP;

	stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn) );
	if ( stat != R_NORMAL )
		return (RT_FAILED);

	dyn.current_mode = CurrentMode;
	
	stat = FileWrite( &file, 1, 1, &dyn, sizeof(dyn) );
	if ( stat != R_NORMAL )
		return (RT_FAILED);

	//TAG per gestire l'info sui sinottici.//
	stat = GetTagIdx(TWN_U_CONTROL_MODE, &tagIdx);
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	
	stat = TagRead(&tagIdx, &tagVal);
	if ( stat != R_NORMAL )
		return (RT_FAILED);


	tagVal.qual.l = 0;
	tagVal.qual.b.q1imp = 1;
	tagVal.value.val_float = (float)CurrentMode;
	
	stat = TagWrite(&tagIdx, &tagVal, TAGWRITE_DIRECT);
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	
	//TAG che passa l'info all'INFI.//
	switch(CurrentMode){
	case M_AUTOMATIC:
		temp = (float)(0);
		break;
	case M_SEMIAUTO:
		temp = (float)(1);
		break;
	case M_PROTECT:
		temp = (float)(2);
		break;
	case M_EMERGEN:
		temp = (float)(4);
		break;
	case M_MAN_SINGLE_CH:
		temp = (float)(5);
		break;
	case M_MAN_WO_INT:
		temp = (float)(3);
		break;
	}

	retSt = VotedTagWriteEx(TWN_OP_MODE, 0, temp, FLT_VALUE);
	if(retSt != RT_NORMAL)
		return(retSt);

	return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		GetDynCurrentOper
**
** Description:		Get Current Operation from Dynamic Parameters
**
** Input:
**
** Output:			Current_ope		- Current Operation
**
** Return:			RETSTAT			- RT_NORMAL	If Ok
**									- RT_FAILED	If Error reading File
*/

RETSTAT GetDynCurrentOper		( int *Current_ope )
{
  RETCOND		stat;
  dynamic_struc	dyn;
  FILEIDX		file;

  file.num = F_DYNP;

  *Current_ope = 0;

  stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn) );
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  *Current_ope = dyn.current_operation;

  return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		SetDynCurrentOper
**
** Description:		Set Current Operation in Dynamic Parameters
**
** Input:			Current_ope		- Current Operation
**
** Output:
**
** Return:			RETSTAT			- RT_NORMAL	If Ok
**									- RT_FAILED	If Error writing File
*/

RETSTAT SetDynCurrentOper	( int Current_ope )
{
  RETCOND		stat;
  dynamic_struc	dyn;
  FILEIDX		file;

  file.num = F_DYNP;

  stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn) );
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  dyn.current_operation = Current_ope;
  stat = FileWrite( &file, 1, 1, &dyn, sizeof(dyn));
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		CheckDynCurrObjt
**
** Description:		Check if an Object Type is currently on Mast
**
** Input:			ObjtIdx		- Object Type Idx
**
** Output:
**
** Return:			RETSTAT		- RT_NORMAL	Object Type present
**								- RT_INVOBJT	If invalid Object Type Idx
**								- RT_FAILED	Object Type NOT present
*/

RETSTAT CheckDynCurrObjt(GENIDX ObjtIdx)
{
  RETCOND		stat;
  dynamic_struc	dyn;
  FILEIDX		file;

  file.num = F_DYNP;

  if ( ( ObjtIdx <= 0 ) || ( ObjtIdx > N_MAX_OBJT ) )
    return (RT_INVOBJT);

  stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn) );
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  if ( dyn.objt_mm.weight[ObjtIdx-1] != 0.0 )
    return (RT_NORMAL);

  return (RT_FAILED);
}

/*------------------------------------------------------------------------------
** Function:		CheckDynCurrNoObjt
**
** Description:		Check if NO one Object is currently on Mast
**
** Input:
**
** Output:
**
** Return:			RETSTAT		- RT_NORMAL	No Object currently on Mast
**								- RT_FAILED	Some Object on Mast
*/

RETSTAT CheckDynCurrNoObjt	( void ){
	RETCOND			stat;
	dynamic_struc	dyn;
	int				i;
	FILEIDX			file;

	file.num = F_DYNP;

	stat = FileRead(&file, 1, 1, &dyn, sizeof(dyn));
	if(stat != R_NORMAL)
		return (RT_FAILED);

	for(i = 0; i < N_MAX_OBJT; i++){
		if(dyn.objt_mm.weight[i] != 0.0)
				return (RT_FAILED);
	}

	return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		ResDynCurrObjt
**
** Description:		Reset Object Type on Mast (Not remove TOOL and PRCA)
**
** Input:
**
** Output:
**
** Return:			RETSTAT		- RT_NORMAL	Operation Ok
**								- RT_FAILED	Operation Ko
*/

RETSTAT ResDynCurrObjt	( void )
{
  RETCOND		stat;
//  RETSTAT		retstat;
  int			ObjtGenCode;
  dynamic_struc	dyn;
  int			i;
  FILEIDX		file;

  file.num = F_DYNP;

  stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn) );
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  for ( i = 0; i < N_MAX_OBJT; i++ ){
	  dyn.objt_mm.weight[i] = 0.0;
	  GetObjtGenericTypeCode ( (GENIDX)(i+1), &ObjtGenCode );
	  dyn.objt_mm.obj[ObjtGenCode] = (GENIDX)0;
	  if ( ObjtGenCode < N_MAX_NAMED_OBJ )
		 dyn.objt_mm.o_id[ObjtGenCode] = (GENIDX)0;
	  
   }

  stat = FileWrite( &file, 1, 1, &dyn, sizeof(dyn));
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		GetCellSituation
**
** Description:		Get Actual Cell Situation using Virtual Coordinate
**
** Input:			VCoo_X		- Virtual Coordinate X
**					VCoo_Y		- Virtual Coordinate Y
** Output:			ObjtCell	- Cell Situation
**
** Return:			RETSTAT		- RT_NORMAL	If Cell Situation exist
**								- RT_FAILED	If Cell Situation not exist
*/

RETSTAT GetCellSituation	( int VCoo_X, int VCoo_Y, OBJTCELL *ObjtCell )
{
  RETCOND	stat;
  t_cell	cell;
  int		i, j;
//  int		FileKey;
  FILEIDX	file;

  file.num = F_DYNP;

  memset ( ObjtCell, 0, sizeof ( OBJTCELL ) );

  for ( i = 0; i < N_MAX_ZONE; i++ )
   {
    file.num = F_CE01 + i;
    for ( j = 0; j < N_MAX_CELL; j++ )
     {
      stat = FileRead( &file, (j+1), 1, &cell, sizeof(t_cell) );
      if ( stat != RT_NORMAL )
        return (RT_FAILED);
        

      if ( ( cell.Virt_X == 0 ) ||
	   ( cell.Virt_Y == 0 ) )
	break;

      if ( ( cell.Virt_X == VCoo_X ) &&
	   ( cell.Virt_Y == VCoo_Y ) )
       {
	memcpy ( ObjtCell, &cell.objt_cell, sizeof ( OBJTCELL ) );
        return (RT_NORMAL);
       }
     }
   }

  return (RT_FAILED);
}

/*------------------------------------------------------------------------------
** Function:		UpdateCellSituation
**
** Description:		Update Cell Situation with new Situation using Virt. Coord.
**
** Input:			VCoo_X			- Virtual Coordinate X
**					VCoo_Y			- Virtual Coordinate Y
**					NewObjtCell		- New Cell Situation
** Output:
**
** Return:			RETSTAT			- RT_NORMAL	If Operation OK
**									- RT_FAILED	If Operation KO
*/

RETSTAT UpdateCellSituation	( int VCoo_X, int VCoo_Y,
				OBJTCELL *NewObjtCell )
{
  RETCOND	stat;
  t_cell	cell;
  int		i, j;
  FILEIDX	file;

  for ( i = 0; i < N_MAX_ZONE; i++ )
   {
    file.num = F_CE01 + i;
    for ( j = 0; j < N_MAX_CELL; j++ )
     {
      stat = FileRead( &file, (j+1), 1, &cell, sizeof(t_cell) );
      if ( stat != RT_NORMAL )
        return (RT_FAILED);
        
      if ( ( cell.Virt_X == 0 ) ||
	   ( cell.Virt_Y == 0 ) )
	break;

      if ( ( cell.Virt_X == VCoo_X ) &&
	   ( cell.Virt_Y == VCoo_Y ) )
       {
	memcpy ( &cell.objt_cell, NewObjtCell, sizeof ( OBJTCELL ) );
        stat = FileWrite( &file, (j+1), 1, &cell, sizeof(t_cell));
        return (RT_NORMAL);
       }
     }
   }

  return (RT_FAILED);
}

/*------------------------------------------------------------------------------
** Function:		GetMastSituation
**
** Description:		Get Actual Mast Situation
**
** Output:			ObjtMast	- Actual Main Mast Situation
**
** Return:			RETSTAT		- RT_NORMAL	
**								- RT_FAILED	
*/

RETSTAT GetMastSituation(OBJTMM *ObjtMast){
	RETCOND				stat;
	dynamic_struc		dyn;
	FILEIDX				file;

	file.num = F_DYNP;

	stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn) );
	if ( stat != R_NORMAL )
		return (RT_FAILED);

	memcpy (ObjtMast, &dyn.objt_mm, sizeof ( OBJTMM ));

	return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		UpdateMastSituation
**
** Description:		Update Mast Situation with new Situation
**
** Input:			NewObjtMast		- New Main Mast Situation
**
** Output:
**
** Return:			RETSTAT			- RT_NORMAL	If Operation OK
**									- RT_FAILED	If Operation KO
*/

RETSTAT UpdateMastSituation	(OBJTMM *NewObjtMast){
	RETCOND			stat;
	dynamic_struc	dyn;
	FILEIDX			file;

	file.num = F_DYNP;

	stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn) );
	if ( stat != R_NORMAL )
		return (RT_FAILED);

	memcpy ( &dyn.objt_mm, NewObjtMast, sizeof ( OBJTMM ) );

	stat = FileWrite( &file, 1, 1, &dyn, sizeof(dyn));
	if ( stat != R_NORMAL )
		return (RT_FAILED);

	return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		CngObjtInCellSituation
**
** Description:		Change Object Type in Cell Situation ( Only for
**					Named Object: CASS, CLUS, HECA )
**
** Input:			ObjtCell		- Cell Situation
**					ObjectIdx		- Object Idx
**					NewObjtIdx		- New Object Type Idx
**
** Output:
**
** Return:			RETSTAT			- RT_NORMAL	
**									- RT_FAILED	
*/

RETSTAT CngObjtInCellSituation	( OBJTCELL *ObjtCell, GENIDX ObjectIdx,
					GENIDX NewObjtIdx )
{
  RETSTAT	stat;
  GENIDX	ObjtIdx;
//  OBJTNAME	GenObjt;
  int		ObjtGenCode;
  int		NewObjtGenCode;
//  int		i;

  /* *** **** ** ****** */
  /* GET TYPE OF OBJECT */
  /* *** **** ** ****** */

  stat = GetObjObjtIdx ( ObjectIdx, &ObjtIdx );
  if ( stat != RT_NORMAL )
    return ( RT_FAILED );

  /* *** ******* **** **** ** ****** */
  /* GET GENERIC TYPE CODE OF OBJECT */
  /* *** ******* **** **** ** ****** */

  stat = GetObjtGenericTypeCode ( ObjtIdx, &ObjtGenCode );
  if ( stat != RT_NORMAL ) 
    return (RT_FAILED);

  /* *** ******* **** **** ** *** ****** */
  /* GET GENERIC TYPE CODE OF NEW OBJECT */
  /* *** ******* **** **** ** *** ****** */

  stat = GetObjtGenericTypeCode ( NewObjtIdx, &NewObjtGenCode );
  if ( stat != RT_NORMAL )
    return (RT_FAILED);

/* ***** **** ****** *** *** ****** *** ** **** ******* **** */
/* CHECK THAT ACTUAL AND NEW OBJECT ARE OF SAME GENERIC TYPE */
/* ***** **** ****** *** *** ****** *** ** **** ******* **** */

  if ( ObjtGenCode != NewObjtGenCode )
    return (RT_FAILED);

  /* ** ******* **** ** ****** ** **** */
  /* IF GENERIC TYPE OF OBJECT IS CASS */
  /* ** ******* **** ** ****** ** **** */

  stat = CheckObjtGenericType ( OBJT_FA, ObjtIdx );
  if ( stat == RT_NORMAL )
   {
    if ( ObjtCell->o_id[OBJT_N_FA] != ObjectIdx )
      return (RT_FAILED);
   }
  else
   {
    /* ** ******* **** ** ****** ** **** */
    /* IF GENERIC TYPE OF OBJECT IS CLUS */
    /* ** ******* **** ** ****** ** **** */

    stat = CheckObjtGenericType ( OBJT_CLUS, ObjtIdx );
    if ( stat == RT_NORMAL )
     {
      if ( ObjtCell->o_id[OBJT_N_CLUS] != ObjectIdx )
        return (RT_FAILED);
     }
    else
     {
      /* ** ******* **** ** ****** ** **** */
      /* IF GENERIC TYPE OF OBJECT IS HECA */
      /* ** ******* **** ** ****** ** **** */

      stat = CheckObjtGenericType ( OBJT_HECA, ObjtIdx );
      if ( stat == RT_NORMAL )
       {
        if ( ObjtCell->o_id[OBJT_N_HECA] != ObjectIdx )
          return ( RT_FAILED );
       }
      else
        return (RT_FAILED);
     }
   }

/* ****** ****** **** *** ** **** */
/* CHANGE OBJECT TYPE IDX IN CELL */
/* ****** ****** **** *** ** **** */

  ObjtCell->obj[ObjtGenCode] = NewObjtIdx;

/* *** *** **** ** **** */
/* PUT NEW DATE IN CELL */
/* *** *** **** ** **** */

  GetApmsTimeEx(&ObjtCell->LastDate);

  return (RT_NORMAL);

}

/*------------------------------------------------------------------------------
** Function:		PutObjectInCellSituation
**
** Description:		Put Object in Cell Situation
**
** Input:			ObjtCell	- Cell Situation
**					ObjectIdx	- Object Idx (present only for CASS CLUS, HECA)
**					ObjtIdx		- Object Type Idx
**
** Output:
**
** Return:			RETSTAT		- RT_NORMAL	
**								- RT_FAILED	
*/

RETSTAT PutObjectInCellSituation(OBJTCELL *ObjtCell, GENIDX ObjectIdx,GENIDX ObjtIdx){
	RETSTAT		stat;
	GENIDX		ObjObjtIdx;
	int			ObjtGenCode;

	/* IF IS A NAMED OBJECT */
	if (ObjectIdx != 0){
		/* GET TYPE OF OBJECT */
		stat = GetObjObjtIdx ( ObjectIdx, &ObjObjtIdx );
		if ( stat != RT_NORMAL )
			return ( RT_FAILED );
		
		/* CHECK THAT OBJECT TYPE AND TYPE OF OBJECT MATCH */
		if ( ObjObjtIdx != ObjtIdx )
			return (RT_FAILED);

		/* GET GENERIC TYPE CODE OF OBJECT TO PUT */
		stat = GetObjtGenericTypeCode  ( ObjObjtIdx, &ObjtGenCode );
		if ( stat != RT_NORMAL )
			return ( stat );
    
		/* IF GENERIC TYPE OF OBJECT IS OF NAMED TYPE */
		if ( ObjtGenCode < N_MAX_NAMED_OBJ ){
			/* CHECK THAT NOT ALREADY EXIST A NAMED OBJECT OF THIS GENERIC TYPE */
			if ( ObjtCell->o_id[ObjtGenCode] != 0 )
				return (RT_FAILED);
			else
				ObjtCell->o_id[ObjtGenCode] = ObjectIdx;
		}
	}
	else{
		/* GET GENERIC TYPE CODE OF OBJECT TO PUT */
		stat = GetObjtGenericTypeCode  ( ObjtIdx, &ObjtGenCode );
		if ( stat != RT_NORMAL )
			return ( stat );
	}
	/* CHECK THAT NOT ALREADY EXIST THIS GENERIC OBJECT TYPE IN CELL */
	if ( ObjtCell->obj[ObjtGenCode] != 0 )
		return ( RT_FAILED );
	/* PUT OBJECT TYPE IN CELL */
		
	ObjtCell->obj[ObjtGenCode] = ObjtIdx;
	
	/* PUT NEW DATE IN CELL */
	GetApmsTimeEx(&ObjtCell->LastDate);

	return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		InsObjectInCellSituation
**
** Description:		Insert Object in Cell Situation (for INSR phase)
**
** Input:			ObjtCell	- Cell Situation
**					ObjectIdx	- Object Idx (present only for CASS CLUS, HECA)
**					ObjtIdx		- Object Type Idx
**
** Output:
**
** Return:			RETSTAT		- RT_NORMAL	
**								- RT_FAILED	
*/

RETSTAT InsObjectInCellSituation	( OBJTCELL *ObjtCell, GENIDX ObjectIdx,
					GENIDX ObjtIdx )

{
  RETSTAT	stat;
  GENIDX	ObjObjtIdx;
//  OBJTNAME	GenObjt;
  int		ObjtGenCode;
//  int		ObjtGenCell;
//  int		i;

/* ** ** * ***** ****** */
/* IF IS A NAMED OBJECT */
/* ** ** * ***** ****** */

  if ( ObjectIdx != 0 )
   {
    ObjObjtIdx = ObjtIdx;

    /* *** ******* **** **** ** ****** ** *** */
    /* GET GENERIC TYPE CODE OF OBJECT TO PUT */
    /* *** ******* **** **** ** ****** ** *** */

    stat = GetObjtGenericTypeCode  ( ObjObjtIdx, &ObjtGenCode );
    if ( stat != RT_NORMAL )
      return ( stat );
    
    /* ** ******* **** ** ****** ** ** ***** **** */
    /* IF GENERIC TYPE OF OBJECT IS OF NAMED TYPE */
    /* ** ******* **** ** ****** ** ** ***** **** */

    if ( ObjtGenCode < N_MAX_NAMED_OBJ )
     {

      /* ***** **** *** ******* ***** * ***** ****** ** **** ******* **** */
      /* CHECK THAT NOT ALREADY EXIST A NAMED OBJECT OF THIS GENERIC TYPE */
      /* ***** **** *** ******* ***** * ***** ****** ** **** ******* **** */

      if ( ObjtCell->o_id[ObjtGenCode] != 0 )
        return (RT_FAILED);
      else
        ObjtCell->o_id[ObjtGenCode] = ObjectIdx;
     }
   }
  else
   {
    /* *** ******* **** **** ** ****** ** *** */
    /* GET GENERIC TYPE CODE OF OBJECT TO PUT */
    /* *** ******* **** **** ** ****** ** *** */

    stat = GetObjtGenericTypeCode  ( ObjtIdx, &ObjtGenCode );
    if ( stat != RT_NORMAL )
      return ( stat );
   }

/* ***** **** *** ******* ***** **** ******* ****** **** ** **** */
/* CHECK THAT NOT ALREADY EXIST THIS GENERIC OBJECT TYPE IN CELL */
/* ***** **** *** ******* ***** **** ******* ****** **** ** **** */

  if ( ObjtCell->obj[ObjtGenCode] != 0 )
    return ( RT_FAILED );
   
/* *** ****** **** ** **** */
/* PUT OBJECT TYPE IN CELL */
/* *** ****** **** ** **** */

  ObjtCell->obj[ObjtGenCode] = ObjtIdx;

/* *** *** **** ** **** */
/* PUT NEW DATE IN CELL */
/* *** *** **** ** **** */

  GetApmsTimeEx(&ObjtCell->LastDate);

  return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		GetObjectFromCellSituation
**
** Description:		Get Object from Cell Situation
**
** Input:			ObjtCell	- Cell Situation
**					ObjectIdx	- Object Idx (present only for CASS CLUS, HECA)
**					ObjtIdx		- Object Type Idx
**
** Output:
**
** Return:			RETSTAT		- RT_NORMAL	
**								- RT_FAILED	
*/

RETSTAT GetObjectFromCellSituation	( OBJTCELL *ObjtCell, GENIDX ObjectIdx,
					GENIDX ObjtIdx )

{
  RETSTAT	stat;
  GENIDX	ObjObjtIdx;
//  OBJTNAME	GenObjt;
  int		ObjtGenCode;
//  int		ObjtGenCell;
//  int		i;

/* ** ** * ***** ****** */
/* IF IS A NAMED OBJECT */
/* ** ** * ***** ****** */

  if ( ObjectIdx != 0 )
   {
    /* *** **** ** ****** */
    /* GET TYPE OF OBJECT */
    /* *** **** ** ****** */

    stat = GetObjObjtIdx ( ObjectIdx, &ObjObjtIdx );
    if ( stat != RT_NORMAL )
      return ( RT_FAILED );

    /* ***** **** ****** **** *** **** ** ****** ***** */
    /* CHECK THAT OBJECT TYPE AND TYPE OF OBJECT MATCH */
    /* ***** **** ****** **** *** **** ** ****** ***** */

    if ( ObjObjtIdx != ObjtIdx )
      return (RT_FAILED);

    /* *** ******* **** **** ** ****** ** *** */
    /* GET GENERIC TYPE CODE OF OBJECT TO GET */
    /* *** ******* **** **** ** ****** ** *** */

    stat = GetObjtGenericTypeCode  ( ObjObjtIdx, &ObjtGenCode );
    if ( stat != RT_NORMAL )
      return ( RT_FAILED );
    
    /* ** ******* **** ** ****** ** ** ***** **** */
    /* IF GENERIC TYPE OF OBJECT IS OF NAMED TYPE */
    /* ** ******* **** ** ****** ** ** ***** **** */

    if ( ObjtGenCode < N_MAX_NAMED_OBJ )
     {
      /* ***** **** ***** * ***** ****** ** **** ******* **** */
      /* CHECK THAT EXIST A NAMED OBJECT OF THIS GENERIC TYPE */
      /* ***** **** ***** * ***** ****** ** **** ******* **** */

      if ( ObjtCell->o_id[ObjtGenCode] != ObjectIdx )
        return (RT_FAILED);
      else
        ObjtCell->o_id[ObjtGenCode] = (GENIDX)0;
     }
   }
  else
   {
    /* *** ******* **** **** ** ****** ** *** */
    /* GET GENERIC TYPE CODE OF OBJECT TO PUT */
    /* *** ******* **** **** ** ****** ** *** */

    stat = GetObjtGenericTypeCode  ( ObjtIdx, &ObjtGenCode );
    if ( stat != RT_NORMAL )
      return ( stat );
   }


/* ***** **** ***** **** ******* ****** **** ** **** */
/* CHECK THAT EXIST THIS GENERIC OBJECT TYPE IN CELL */
/* ***** **** ***** **** ******* ****** **** ** **** */

  if ( ObjtCell->obj[ObjtGenCode] != ObjtIdx )
    return ( RT_FAILED );
   
/* *** ****** **** **** **** */
/* GET OBJECT TYPE FROM CELL */
/* *** ****** **** **** **** */

  ObjtCell->obj[ObjtGenCode] = (GENIDX)0;

/* *** *** **** ** **** */
/* PUT NEW DATE IN CELL */
/* *** *** **** ** **** */

  GetApmsTimeEx(&ObjtCell->LastDate);

  return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		PutObjectInMastSituation
**
** Description:		Put Object in Mast Situation
**
** Input:			ObjtMast	- Mast Situation
**					ObjtIdx		- Object Type Idx
**
** Output:
**
** Return:			RETSTAT		- RT_NORMAL	
**								- RT_FAILED	
*/

RETSTAT PutObjectInMastSituation(OBJTMM *ObjtMast, GENIDX ObjtIdx)

{
  RETSTAT	stat;
  float		DimX, DimY, DimZ;

  stat = GetObjtDimensions ( ObjtIdx, &DimX, &DimY, &DimZ );
  if ( stat != RT_NORMAL )
    return ( RT_FAILED );

  if ( ObjtMast->weight[ObjtIdx-1] != 0.0 )
    return (RT_FAILED);

  ObjtMast->weight[ObjtIdx-1] = DimZ;
  
  return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		GetObjectFromMastSituation
**
** Description:		Get Object from Mast Situation
**
** Input:			ObjtMast	- Mast Situation
**					ObjtIdx		- Object Type Idx
**
** Output:
**
** Return:			RETSTAT		- RT_NORMAL	
**								- RT_FAILED	
*/

RETSTAT GetObjectFromMastSituation	( OBJTMM *ObjtMast, GENIDX ObjtIdx )

{
  RETSTAT	stat;
  float		DimX, DimY, DimZ;

  stat = GetObjtDimensions ( ObjtIdx, &DimX, &DimY, &DimZ );
  if ( stat != RT_NORMAL )
    return ( RT_FAILED );

  if ( ObjtMast->weight[ObjtIdx-1] == 0.0 )
    return (RT_FAILED);

  ObjtMast->weight[ObjtIdx-1] = 0.0;
  
  return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		InsertObject
**
** Description:		Insert Object in Cell and Remove from Mast
**
** Input:			ObjtCell	- Cell Situation
**					ObjtMast	- Mast Situation
**					ObjectIdx	- Object Idx (present only for CASS CLUS, HECA)
**					ObjtIdx		- Object Type Idx
**
** Output:
**
** Return:			RETSTAT		- RT_NORMAL	
**								- RT_FAILED	
*/

RETSTAT InsertObject(OBJTCELL *ObjtCell, OBJTMM *ObjtMast, GENIDX ObjectIdx, GENIDX ObjtIdx, int cellType){
	RETSTAT		stat;
	GENIDX		ObjObjtIdx;
	GENIDX		AppObjtIdx;
	int			ObjtGenCode;

	if(ObjectIdx != 0){
		/* GET TYPE OF OBJECT */
		stat = GetObjObjtIdx(ObjectIdx, &ObjObjtIdx);
		if(stat != RT_NORMAL)
			return(RT_FAILED);
		
		/* CHECK THAT OBJECT TYPE AND TYPE OF OBJECT MATCH */
		if(ObjObjtIdx != ObjtIdx)
			return (RT_FAILED);

		/* GET GENERIC TYPE CODE OF OBJECT TO INSERT */
		stat = GetObjtGenericTypeCode (ObjObjtIdx, &ObjtGenCode);
		if(stat != RT_NORMAL)
			return(RT_FAILED);
    
		/* IF GENERIC TYPE OF OBJECT IS OF NAMED TYPE */
		if(ObjtGenCode < N_MAX_NAMED_OBJ){
			
			/* CHECK THAT EXIST A NAMED OBJECT OF THIS GENERIC TYPE */
			if ( ObjtMast->o_id[ObjtGenCode] != ObjectIdx )
				return (RT_FAILED);

			/* IF WE ARE INSERTING HECA, INSERT ALSO COVER, CASS(IF EXISTS) */
			if(ObjtGenCode == OBJT_N_HECA){
				if(cellType != CELL_HECA)
					return(RT_FAILED);

				if(ObjtCell->obj[OBJT_N_HECA] != 0)
					return(RT_FAILED);
			
				ObjtCell->o_id[OBJT_N_HECA] = ObjtMast->o_id[OBJT_N_HECA];
				GetObjObjtIdx(ObjtMast->o_id[OBJT_N_HECA], &AppObjtIdx);
				ObjtCell->obj[OBJT_N_HECA] = AppObjtIdx;
				ObjtMast->obj[OBJT_N_HECA] = (GENIDX)0;
				ObjtMast->o_id[OBJT_N_HECA] = (GENIDX)0;
				GetObjectFromMastSituation(ObjtMast, AppObjtIdx);
				if(ObjtMast->o_id[OBJT_N_FA] != 0){
					ObjtCell->o_id[OBJT_N_FA] = ObjtMast->o_id[OBJT_N_FA];
					GetObjObjtIdx(ObjtMast->o_id[OBJT_N_FA], &AppObjtIdx);
					ObjtCell->obj[OBJT_N_FA] = AppObjtIdx;
					ObjtMast->obj[OBJT_N_FA] = (GENIDX)0;
					ObjtMast->o_id[OBJT_N_FA] = (GENIDX)0;
					GetObjectFromMastSituation(ObjtMast, AppObjtIdx);
				}
				if(ObjtMast->obj[OBJT_N_COVER] != 0){
					ObjtCell->obj[OBJT_N_COVER] = ObjtMast->obj[OBJT_N_COVER];
					ObjtCell->o_id[OBJT_N_COVER] = ObjtMast->o_id[OBJT_N_COVER];
					GetObjectFromMastSituation(ObjtMast, ObjtMast->obj[OBJT_N_COVER]);
					ObjtMast->obj[OBJT_N_COVER] = (GENIDX)0;
					ObjtMast->o_id[OBJT_N_COVER] = (GENIDX)0;
				}
				if(ObjtMast->obj[OBJT_N_CLUS] != 0){
					ObjtCell->obj[OBJT_N_CLUS] = ObjtMast->obj[OBJT_N_CLUS];
					ObjtCell->o_id[OBJT_N_CLUS] = ObjtMast->o_id[OBJT_N_CLUS];
					GetObjectFromMastSituation(ObjtMast, ObjtMast->obj[OBJT_N_CLUS]);
					ObjtMast->obj[OBJT_N_CLUS] = (GENIDX)0;
					ObjtMast->o_id[OBJT_N_CLUS] = (GENIDX)0;
				}
			}

			/* IF WE ARE INSERTING CASS, INSERT ALSO CLUS (IF EXIST) */
			if (ObjtGenCode == OBJT_N_FA){
				if(cellType == CELL_FA){
					if((ObjtCell->obj[OBJT_N_FA] != 0) || (ObjtCell->obj[OBJT_N_CLUS_CAS] != 0) || (ObjtCell->obj[OBJT_N_CLUS_FRM] != 0))
						return(RT_FAILED);
				}
				else if(cellType == CELL_HECA){
					if((ObjtCell->obj[OBJT_N_FA] != 0) || ((ObjtCell->obj[OBJT_N_HECA] != 0) && (ObjtCell->obj[OBJT_N_COVER] != 0)) || (ObjtCell->obj[OBJT_N_HECA] == 0))
						return(RT_FAILED);
				}
				else if(cellType == CELL_FFDS_CASK){
					if((ObjtCell->obj[OBJT_N_FA] != 0) || (ObjtCell->obj[OBJT_N_FFDS_COV] != 0))
						return(RT_FAILED);
				}
				else
					return(RT_FAILED);
				
				ObjtCell->o_id[OBJT_N_FA] = ObjtMast->o_id[OBJT_N_FA];
				GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_FA], &AppObjtIdx );
				ObjtCell->obj [OBJT_N_FA] = AppObjtIdx;
				ObjtMast->obj [OBJT_N_FA] = (GENIDX)0;
				ObjtMast->o_id[OBJT_N_FA] = (GENIDX)0;
				GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );

				if ( ObjtMast->o_id[OBJT_N_CLUS] != 0 ){
					ObjtCell->o_id[OBJT_N_CLUS] = ObjtMast->o_id[OBJT_N_CLUS];
					GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_CLUS], &AppObjtIdx );
					ObjtCell->obj [OBJT_N_CLUS] = AppObjtIdx;
					ObjtMast->obj [OBJT_N_CLUS] = (GENIDX)0;
					ObjtMast->o_id[OBJT_N_CLUS] = (GENIDX)0;
					GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );
				}
			}

			/* IF WE ARE INSERTING CLUS */
			if(ObjtGenCode == OBJT_N_CLUS){
				if(cellType == CELL_FA){
					if((ObjtCell->obj[OBJT_N_FA] == 0) && (ObjtCell->obj[OBJT_N_CLUS_FRM] == 0))
						return(RT_FAILED);
					else if(ObjtCell->obj[OBJT_N_CLUS] != 0)
						return(RT_FAILED);
				}
				else if(cellType == CELL_COVER_PLUG){
					if((ObjtCell->obj[OBJT_N_COVER] != 0) || (ObjtCell->obj[OBJT_N_FFDS_COV] != 0))
						return(RT_FAILED);
					else if(ObjtCell->obj[OBJT_N_CLUS] != 0)
						return(RT_FAILED);
				}
				else
					return(RT_FAILED);

				if(ObjtCell->obj[OBJT_N_CLUS] != 0)
					return(RT_FAILED);
				ObjtCell->o_id[OBJT_N_CLUS] = ObjtMast->o_id[OBJT_N_CLUS];
				GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_CLUS], &AppObjtIdx );
				ObjtCell->obj [OBJT_N_CLUS] = AppObjtIdx;
				ObjtMast->obj [OBJT_N_CLUS] = (GENIDX)0;
				ObjtMast->o_id[OBJT_N_CLUS] = (GENIDX)0;
				GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );
			}

			if(ObjtGenCode == OBJT_N_COVER){
				if(cellType == CELL_HECA){
					if((ObjtCell->obj[OBJT_N_HECA] == 0) || ((ObjtCell->obj[OBJT_N_HECA] != 0) && (ObjtCell->obj[OBJT_N_COVER] != 0)))
						return(RT_FAILED);
				}
				else if((cellType == CELL_COVER_PLUG) || (cellType == CELL_COVER_PLUG_EX)){
					if((ObjtCell->obj[OBJT_N_COVER] != 0) || (ObjtCell->obj[OBJT_N_FFDS_COV] != 0))
						return(RT_FAILED);
				}
				else
					return(RT_FAILED);

				if(ObjtCell->obj[OBJT_N_COVER] != 0)
					return(RT_FAILED);
				ObjtCell->o_id[OBJT_N_COVER] = ObjtMast->o_id[OBJT_N_COVER];
				GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_COVER], &AppObjtIdx );
				ObjtCell->obj [OBJT_N_COVER] = AppObjtIdx;
				ObjtMast->obj [OBJT_N_COVER] = (GENIDX)0;
				ObjtMast->o_id[OBJT_N_COVER] = (GENIDX)0;
				GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );
			}
			if(ObjtGenCode == OBJT_N_CLUS_FRM){
				if(cellType != CELL_FA)
					return(RT_FAILED);
				if((ObjtCell->obj[OBJT_N_CLUS_FRM] != 0) || (ObjtCell->obj[OBJT_N_FA] != 0))
					return(RT_FAILED);
				ObjtCell->o_id[OBJT_N_CLUS_FRM] = ObjtMast->o_id[OBJT_N_CLUS_FRM];
				GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_CLUS_FRM], &AppObjtIdx );
				ObjtCell->obj [OBJT_N_CLUS_FRM] = AppObjtIdx;
				ObjtMast->obj [OBJT_N_CLUS_FRM] = (GENIDX)0;
				ObjtMast->o_id[OBJT_N_CLUS_FRM] = (GENIDX)0;
				GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );

				if ( ObjtMast->o_id[OBJT_N_CLUS] != 0 ){
					ObjtCell->o_id[OBJT_N_CLUS] = ObjtMast->o_id[OBJT_N_CLUS];
					GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_CLUS], &AppObjtIdx );
					ObjtCell->obj [OBJT_N_CLUS] = AppObjtIdx;
					ObjtMast->obj [OBJT_N_CLUS] = (GENIDX)0;
					ObjtMast->o_id[OBJT_N_CLUS] = (GENIDX)0;
					GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );
				}
			}	
		}
		else{
			/*
			
			stat = GetObjtGenericTypeCode (ObjtIdx, &ObjtGenCode);
			if(stat != RT_NORMAL)
				return(stat);

			
			if(ObjtMast->obj[ObjtGenCode] != ObjtIdx)
				return(RT_FAILED);
			
			
			ObjtMast->obj[ObjtGenCode] = (GENIDX)0;
			GetObjectFromMastSituation ( ObjtMast, ObjtIdx );
			ObjtCell->obj[ObjtGenCode] = ObjtIdx;
			*/
			return(RT_FAILED);
		}
	}
	
	/* ** ** *** * ***** ****** */
	/* IF IS NOT A NAMED OBJECT */
	/* ** ** *** * ***** ****** */
	else{
		/* *** ******* **** **** ** ****** ** ****** */
		/* GET GENERIC TYPE CODE OF OBJECT TO INSERT */
		/* *** ******* **** **** ** ****** ** ****** */
		stat = GetObjtGenericTypeCode  ( ObjtIdx, &ObjtGenCode );
		if ( stat != RT_NORMAL )
			return ( stat );

		if(ObjtMast->obj[ObjtGenCode] != ObjtIdx)
			return(RT_FAILED);
	
		/* IF WE ARE INSERTING CLUSTER FRAME INSERT ALSO CLUS (IF EXIST) */
		

		if(ObjtGenCode == OBJT_N_CLUS_CAS){
			if(cellType != CELL_COVER_PLUG_EX)
				return(RT_FAILED);
			else if((ObjtCell->obj[OBJT_N_FFDS_COV] != 0) || (ObjtCell->obj[OBJT_N_COVER] != 0))
				return(RT_FAILED);
		}
		else if(ObjtGenCode == OBJT_N_FFDS_COV){
			if((cellType != CELL_COVER_PLUG_EX) && (cellType != CELL_COVER_PLUG) && (cellType != CELL_FFDS_CASK))
				return(RT_FAILED);
			else if((ObjtCell->obj[OBJT_N_FFDS_COV] != 0) || (ObjtCell->obj[OBJT_N_COVER] != 0))
				return(RT_FAILED);
		}

		ObjtMast->obj[ObjtGenCode] = (GENIDX)0;
		GetObjectFromMastSituation ( ObjtMast, ObjtIdx );
		ObjtCell->obj[ObjtGenCode] = ObjtIdx;
	}


	/* *** *** **** ** **** */
	/* PUT NEW DATE IN CELL */
	/* *** *** **** ** **** */

	GetApmsTimeEx(&ObjtCell->LastDate);

	return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		RemoveObject
**
** Description:		Remove Object from Cell and Insert in Mast
**
** Input:			ObjtCell	- Cell Situation
**					ObjtMast	- Mast Situation
**					ObjectIdx	- Object Idx (present only for CASS CLUS, HECA)
**					ObjtIdx		- Object Type Idx
**
** Output:
**
** Return:			RETSTAT		- RT_NORMAL	
**								- RT_FAILED	
*/

RETSTAT RemoveObject(OBJTCELL *ObjtCell, OBJTMM *ObjtMast, GENIDX ObjectIdx, GENIDX ObjtIdx ){
	RETSTAT		stat;
	GENIDX		ObjObjtIdx;
	//OBJTNAME	GenObjt;
	int			ObjtGenCode;

		
	// IF IS A NAMED OBJECT //
	if ( ObjectIdx != 0 ){
		// GET TYPE OF OBJECT //
		stat = GetObjObjtIdx ( ObjectIdx, &ObjObjtIdx );
		if ( stat != RT_NORMAL )
		return ( RT_FAILED );
		
		/* CHECK THAT OBJECT TYPE AND TYPE OF OBJECT MATCH */
		if ( ObjObjtIdx != ObjtIdx )
			return (RT_FAILED);
		
		/* GET GENERIC TYPE CODE OF OBJECT TO REMOVE */
		stat = GetObjtGenericTypeCode  ( ObjObjtIdx, &ObjtGenCode );
		if ( stat != RT_NORMAL )
			return ( RT_FAILED );
    
		/* IF GENERIC TYPE OF OBJECT IS OF NAMED TYPE */
		if ( ObjtGenCode < N_MAX_NAMED_OBJ ){
			/* CHECK THAT EXIST A NAMED OBJECT OF THIS GENERIC TYPE */
			if ( ObjtCell->o_id[ObjtGenCode] != ObjectIdx )
				return (RT_FAILED);

			/* IF WE ARE REMOVING HECA, REMOVE ALSO CASS AND CLUS (IF EXISTS) */
			if ( ObjtGenCode == OBJT_N_HECA ){
				if((ObjtMast->obj[OBJT_N_FA] != 0)			|| 
				   (ObjtMast->obj[OBJT_N_CLUS] != 0)		|| 
				   (ObjtMast->obj[OBJT_N_HECA] != 0)		|| 
				   (ObjtMast->obj[OBJT_N_COVER] != 0)		|| 
				   (ObjtMast->obj[OBJT_N_CLUS_CAS] != 0)	||
				   (ObjtMast->obj[OBJT_N_CLUS_FRM] != 0)	|| 
				   (ObjtMast->obj[OBJT_N_FFDS_COV] != 0))
				   return(RT_FAILED);

				if(ObjtCell->obj[OBJT_N_COVER] == 0)
					return(RT_FAILED);

				ObjtMast->o_id[OBJT_N_HECA] = ObjtCell->o_id[OBJT_N_HECA];
				ObjtMast->obj [OBJT_N_HECA] = ObjtCell->obj [OBJT_N_HECA];
				PutObjectInMastSituation ( ObjtMast, ObjtCell->obj[OBJT_N_HECA] );
				ObjtCell->o_id[OBJT_N_HECA] = (GENIDX)0;
				ObjtCell->obj [OBJT_N_HECA] = (GENIDX)0;

				if ( ObjtCell->o_id[OBJT_N_FA] != 0 ){
					ObjtMast->o_id[OBJT_N_FA] = ObjtCell->o_id[OBJT_N_FA];
					ObjtMast->obj [OBJT_N_FA] = ObjtCell->obj [OBJT_N_FA];
					PutObjectInMastSituation ( ObjtMast, ObjtCell->obj[OBJT_N_FA] );
					ObjtCell->o_id[OBJT_N_FA] = (GENIDX)0;
					ObjtCell->obj [OBJT_N_FA] = (GENIDX)0;
				}

				if ( ObjtCell->obj[OBJT_N_COVER] != 0 ){
					ObjtMast->obj[OBJT_N_COVER] = ObjtCell->obj[OBJT_N_COVER];
					ObjtMast->o_id[OBJT_N_COVER] = ObjtCell->o_id[OBJT_N_COVER];
					PutObjectInMastSituation ( ObjtMast, ObjtCell->obj[OBJT_N_COVER] );
					ObjtCell->obj[OBJT_N_COVER] = (GENIDX)0;
					ObjtCell->o_id[OBJT_N_COVER] = (GENIDX)0;
				}

				if ( ObjtCell->obj[OBJT_N_CLUS] != 0 ){
					ObjtMast->obj[OBJT_N_CLUS] = ObjtCell->obj[OBJT_N_CLUS];
					ObjtMast->o_id[OBJT_N_CLUS] = ObjtCell->o_id[OBJT_N_CLUS];
					PutObjectInMastSituation ( ObjtMast, ObjtCell->obj[OBJT_N_CLUS] );
					ObjtCell->obj[OBJT_N_CLUS] = (GENIDX)0;
					ObjtCell->o_id[OBJT_N_CLUS] = (GENIDX)0;
				}
			}

			/* IF WE ARE REMOVING CASS, REMOVE ALSO CLUS (IF EXIST) */
			if (ObjtGenCode == OBJT_N_FA){
				if((ObjtMast->obj[OBJT_N_FA] != 0)			|| 
				   (ObjtMast->obj[OBJT_N_CLUS] != 0)		|| 
				   (ObjtMast->obj[OBJT_N_HECA] != 0)		|| 
				   (ObjtMast->obj[OBJT_N_COVER] != 0)		|| 
				   (ObjtMast->obj[OBJT_N_CLUS_CAS] != 0)	|| 
				   (ObjtMast->obj[OBJT_N_CLUS_FRM] != 0)	|| 
				   (ObjtMast->obj[OBJT_N_FFDS_COV] != 0))
				   return(RT_FAILED);
				ObjtMast->o_id[OBJT_N_FA] = ObjtCell->o_id[OBJT_N_FA];
				ObjtMast->obj [OBJT_N_FA] = ObjtCell->obj [OBJT_N_FA];
				PutObjectInMastSituation ( ObjtMast, ObjtCell->obj[OBJT_N_FA] );
				ObjtCell->o_id[OBJT_N_FA] = (GENIDX)0;
				ObjtCell->obj [OBJT_N_FA] = (GENIDX)0;

				if ( ObjtCell->o_id[OBJT_N_CLUS] != 0 ){
					ObjtMast->o_id[OBJT_N_CLUS] = ObjtCell->o_id[OBJT_N_CLUS];
					ObjtMast->obj [OBJT_N_CLUS] = ObjtCell->obj [OBJT_N_CLUS];
					PutObjectInMastSituation ( ObjtMast, ObjtCell->obj[OBJT_N_CLUS] );
					ObjtCell->o_id[OBJT_N_CLUS] = (GENIDX)0;
					ObjtCell->obj [OBJT_N_CLUS] = (GENIDX)0;
				}
			}

			

			/* IF WE ARE REMOVING CLUS */
			if ( ObjtGenCode == OBJT_N_CLUS ){
				if((ObjtMast->obj[OBJT_N_FA] != 0)			|| 
				   (ObjtMast->obj[OBJT_N_CLUS] != 0)		|| 
				   (ObjtMast->obj[OBJT_N_HECA] != 0)		|| 
				   (ObjtMast->obj[OBJT_N_COVER] != 0)		||
				   (ObjtMast->obj[OBJT_N_CLUS_FRM] != 0)	|| 
				   (ObjtMast->obj[OBJT_N_FFDS_COV] != 0))
				   return(RT_FAILED);
				if(ObjtMast->obj [OBJT_N_CLUS_CAS] == 0)
				   return(RT_FAILED);
				ObjtMast->o_id[OBJT_N_CLUS] = ObjtCell->o_id[OBJT_N_CLUS];
				ObjtMast->obj [OBJT_N_CLUS] = ObjtCell->obj [OBJT_N_CLUS];
				PutObjectInMastSituation ( ObjtMast, ObjtCell->obj[OBJT_N_CLUS] );
				ObjtCell->o_id[OBJT_N_CLUS] = (GENIDX)0;
				ObjtCell->obj [OBJT_N_CLUS] = (GENIDX)0;
			}

			if ( ObjtGenCode == OBJT_N_COVER){
				if((ObjtMast->obj[OBJT_N_FA] != 0)			|| 
				   (ObjtMast->obj[OBJT_N_CLUS] != 0)		|| 
				   (ObjtMast->obj[OBJT_N_HECA] != 0)		|| 
				   (ObjtMast->obj[OBJT_N_COVER] != 0)		|| 
				   (ObjtMast->obj[OBJT_N_CLUS_FRM] != 0)	||
				   (ObjtMast->obj[OBJT_N_FFDS_COV] != 0))
				   return(RT_FAILED);
				ObjtMast->o_id[OBJT_N_COVER] = ObjtCell->o_id[OBJT_N_COVER];
				ObjtMast->obj [OBJT_N_COVER] = ObjtCell->obj [OBJT_N_COVER];
				PutObjectInMastSituation ( ObjtMast, ObjtCell->obj[OBJT_N_COVER] );
				ObjtCell->o_id[OBJT_N_COVER] = (GENIDX)0;
				ObjtCell->obj [OBJT_N_COVER] = (GENIDX)0;
			}

			if ( ObjtGenCode == OBJT_N_CLUS_FRM){
				if((ObjtMast->obj[OBJT_N_FA] != 0)			|| 
				   (ObjtMast->obj[OBJT_N_CLUS] != 0)		|| 
				   (ObjtMast->obj[OBJT_N_HECA] != 0)		|| 
				   (ObjtMast->obj[OBJT_N_COVER] != 0)		|| 
				   (ObjtMast->obj[OBJT_N_CLUS_CAS] != 0)	||
				   (ObjtMast->obj[OBJT_N_CLUS_FRM] != 0)	||
				   (ObjtMast->obj[OBJT_N_FFDS_COV] != 0))
				   return(RT_FAILED);
				ObjtMast->o_id[OBJT_N_CLUS_FRM] = ObjtCell->o_id[OBJT_N_CLUS_FRM];
				ObjtMast->obj [OBJT_N_CLUS_FRM] = ObjtCell->obj [OBJT_N_CLUS_FRM];
				PutObjectInMastSituation ( ObjtMast, ObjtCell->obj[OBJT_N_CLUS_FRM] );
				ObjtCell->o_id[OBJT_N_CLUS_FRM] = (GENIDX)0;
				ObjtCell->obj [OBJT_N_CLUS_FRM] = (GENIDX)0;

				if ( ObjtCell->o_id[OBJT_N_CLUS] != 0 ){
					ObjtMast->o_id[OBJT_N_CLUS] = ObjtCell->o_id[OBJT_N_CLUS];
					ObjtMast->obj [OBJT_N_CLUS] = ObjtCell->obj [OBJT_N_CLUS];
					PutObjectInMastSituation ( ObjtMast, ObjtCell->obj[OBJT_N_CLUS] );
					ObjtCell->o_id[OBJT_N_CLUS] = (GENIDX)0;
					ObjtCell->obj [OBJT_N_CLUS] = (GENIDX)0;
				}
			}
		}
		else{
			/*
			stat = GetObjtGenericTypeCode  ( ObjtIdx, &ObjtGenCode );
			if ( stat != RT_NORMAL )
				return ( stat );

			
			if ( ObjtCell->obj[ObjtGenCode] != ObjtIdx )
				return ( RT_FAILED );
   
			
			ObjtMast->obj[ObjtGenCode] = ObjtCell->obj [ObjtGenCode];
			PutObjectInMastSituation ( ObjtMast, ObjtCell->obj[ObjtGenCode] );
			ObjtCell->obj[ObjtGenCode] = (GENIDX)0;
			*/
			return(RT_FAILED);
		}
	}

	/* IF IS NOT A NAMED OBJECT */
	else{
		/* GET GENERIC TYPE CODE OF OBJECT TO REMOVE */
		stat = GetObjtGenericTypeCode  ( ObjtIdx, &ObjtGenCode );
		if ( stat != RT_NORMAL )
			return ( stat );

		/* CHECK THAT EXIST THIS GENERIC OBJECT TYPE IN CELL */
		if ( ObjtCell->obj[ObjtGenCode] != ObjtIdx )
			return ( RT_FAILED );

		/* IF WE ARE REMOVING CLUS_FRM, REMOVE ALSO CLUS (IF EXIST) */
		
		if(ObjtGenCode == OBJT_N_CLUS_CAS){
			if((ObjtCell->obj[OBJT_N_FFDS_COV] != 0) || (ObjtCell->obj[OBJT_N_COVER] != 0))
				return(RT_FAILED);
		}
		/*
		else if(ObjtGenCode == OBJT_N_FFDS_COV){
			if((ObjtCell->obj[OBJT_N_FFDS_COV] != 0) || (ObjtCell->obj[OBJT_N_COVER] != 0))
				return(RT_FAILED);
		}
	*/
		if((ObjtMast->obj [OBJT_N_FA] != 0)		|| 
			   (ObjtMast->obj [OBJT_N_CLUS] != 0)		|| 
			   (ObjtMast->obj [OBJT_N_HECA] != 0)		|| 
			   (ObjtMast->obj [OBJT_N_COVER] != 0)		|| 
			   (ObjtMast->obj [OBJT_N_CLUS_CAS] != 0)	|| 
			   (ObjtMast->obj [OBJT_N_CLUS_FRM] != 0)	|| 
			   (ObjtMast->obj [OBJT_N_FFDS_COV] != 0))
				return(RT_FAILED);
		
		/* REMOVE OBJECT TYPE FROM CELL */
		ObjtMast->obj[ObjtGenCode] = ObjtCell->obj [ObjtGenCode];
		PutObjectInMastSituation ( ObjtMast, ObjtCell->obj[ObjtGenCode] );
		ObjtCell->obj[ObjtGenCode] = (GENIDX)0;
	}
	
	/* PUT NEW DATE IN CELL */
	GetApmsTimeEx(&ObjtCell->LastDate);
	return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		InitMicroCmdExe
**
** Description:		Initialize Micro Cmd Exe File for new Phase
**
** Input:
**
** Output:
**
** Return:			RETSTAT		- RT_NORMAL	
**								- RT_FAILED	
*/

RETSTAT	InitMicroCmdExe	( void ){

	RETCOND			stat;
	micro_cmd_exe	micro_exe; 
	int				i;
	FILEIDX			file;

	InitMice(&micro_exe);

	file.num = F_MICE;
	for ( i = 1; i <= N_MAX_MICENR; i++ ) { 
		stat = FileWrite ( &file, i, 1, &micro_exe, sizeof(micro_exe) ); 
	} 

	return (RT_NORMAL);
}

/*------------------------------------------------------------------------------------------
** Function:		Get_Mast_Info
**
** Description:		Get Mast Actual Position from TAGS in DB.
**
** Input:			MastType	-	mast type to get info	-	0	-	Main Mast
**																1	-	New Fuel Rod (NFR)
**																2	-	Spent Fuel Rod (SFR)
** Output:			Info		-	mast_info struct containing mast data.	
**
** Return:			RETSTAT		-	RT_NORMAL	
**								-	RT_FAILED	
**
** Added by Matteo Tavella 08/11/2001
*/

RETSTAT Get_Mast_Info(mast_info *Info){
	/*
	RETCOND		stat;
	TAGIDX		tagidx;
	TAGVALEX	tagval;
	
	
	//Prelevo la posizione X del Main mast.//
	stat = GetTagIdx("BRIDGE_X", &tagidx );
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	stat = TagRead (&tagidx, &tagval);    
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	if(!((GtBit(tagval.qual.qf[0], Q1IMP ) == 1) &&
		(GtBit(tagval.qual.qf[0], Q1OLDD) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1OFSC) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1HCFV) == 0)))
		return (RT_FAILED);
	else
		Info->PosX = tagval.value.val_float;
	//Prelevo la posizione Y del Main mast.//
	stat = GetTagIdx("TROLLEY_Y", &tagidx );
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	stat = TagRead (&tagidx, &tagval);    
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	if(!((GtBit(tagval.qual.qf[0], Q1IMP) == 1) &&
		(GtBit(tagval.qual.qf[0], Q1OLDD) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1OFSC) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1HCFV) == 0)))
		return (RT_FAILED);
	else
		Info->PosY = tagval.value.val_float;

	//Prelevo la posizione Y del Cantilever.//
	stat = GetTagIdx("CANT_POS", &tagidx );
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	stat = TagRead (&tagidx, &tagval);    
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	if(!((GtBit(tagval.qual.qf[0], Q1IMP) == 1) &&
		(GtBit(tagval.qual.qf[0], Q1OLDD) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1OFSC) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1HCFV) == 0)))
		return (RT_FAILED);
	else
		Info->PosC = tagval.value.val_float;

	//Prelevo la posizione Z dell'FA gripper del Main mast.//
	stat = GetTagIdx("M_ROD_POS", &tagidx );
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	stat = TagRead (&tagidx, &tagval);    
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	if(!((GtBit(tagval.qual.qf[0], Q1IMP) == 1) &&
	    (GtBit(tagval.qual.qf[0], Q1OLDD) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1OFSC) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1HCFV) == 0)))
		return (RT_FAILED);
	else
		Info->PosZ[MAIN_MAST].FAG_PosZ = tagval.value.val_float;
	//Prelevo la posizione Z del cluster gripper del Main mast.//
	stat = GetTagIdx("MR_CL_POS", &tagidx );
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	stat = TagRead (&tagidx, &tagval);    
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	if(!((GtBit(tagval.qual.qf[0], Q1IMP) == 1) &&
		(GtBit(tagval.qual.qf[0], Q1OLDD) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1OFSC) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1HCFV) == 0)))
		return (RT_FAILED);
	else
		Info->PosZ[MAIN_MAST].CLG_PosZ = tagval.value.val_float;
	//Prelevo l'angolo di rotazione del Main mast.//
	stat = GetTagIdx("M_ROD_ANGLE", &tagidx );
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	stat = TagRead (&tagidx, &tagval);    
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	if(!((GtBit(tagval.qual.qf[0], Q1IMP) == 1) &&
		(GtBit(tagval.qual.qf[0], Q1OLDD) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1OFSC) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1HCFV) == 0)))
		return (RT_FAILED);
	else
		Info->Angle = tagval.value.val_float;
	//Prelevo lo stato dell'FA gripper latch del Main mast.//
	stat = GetTagIdx("MR_FIX_OP", &tagidx );
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	stat = TagRead (&tagidx, &tagval);    
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	if(!((GtBit(tagval.qual.qf[0], Q1IMP) == 1) &&
		(GtBit(tagval.qual.qf[0], Q1OLDD) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1OFSC) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1HCFV) == 0)))
		return (RT_FAILED);
	else
		Info->PosZ[MAIN_MAST].FAL_Open = (int)tagval.value.val_di;
	
	//Prelevo la posizione Z dell'FA gripper del New fuel rod.//
	stat = GetTagIdx("NFR_POS", &tagidx );
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	stat = TagRead (&tagidx, &tagval);    
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	if(!((GtBit(tagval.qual.qf[0], Q1IMP) == 1) &&
		(GtBit(tagval.qual.qf[0], Q1OLDD) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1OFSC) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1HCFV) == 0)))
		return (RT_FAILED);
	else
		Info->PosZ[NEW_FUEL_ROD].FAG_PosZ = tagval.value.val_float;
	//Prelevo la posizione Z del cluster gripper del New fuel rod.//
	stat = GetTagIdx("NFR_CL_POS", &tagidx );
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	stat = TagRead (&tagidx, &tagval);    
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	if(!((GtBit(tagval.qual.qf[0], Q1IMP) == 1) &&
		(GtBit(tagval.qual.qf[0], Q1OLDD) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1OFSC) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1HCFV) == 0)))
		return (RT_FAILED);
	else
	Info->PosZ[NEW_FUEL_ROD].CLG_PosZ = tagval.value.val_float;

	//Prelevo lo stato dell'FA gripper latch del New fuel rod.//
	stat = GetTagIdx("NFR_FIX_OP", &tagidx );
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	stat = TagRead (&tagidx, &tagval);    
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	if(!((GtBit(tagval.qual.qf[0], Q1IMP) == 1) &&
		(GtBit(tagval.qual.qf[0], Q1OLDD) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1OFSC) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1HCFV) == 0)))
		return (RT_FAILED);
	else
		Info->PosZ[NEW_FUEL_ROD].FAL_Open = (int)tagval.value.val_di;
	
	//Prelevo la posizione Z dell'FA gripper del Spent fuel rod.//
	stat = GetTagIdx("SFR_POS", &tagidx );
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	stat = TagRead (&tagidx, &tagval);    
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	if(!((GtBit(tagval.qual.qf[0], Q1IMP) == 1) &&
		(GtBit(tagval.qual.qf[0], Q1OLDD) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1OFSC) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1HCFV) == 0)))
		return (RT_FAILED);
	else
		Info->PosZ[SPENT_FUEL_ROD].FAG_PosZ = tagval.value.val_float;
	
	//Prelevo la posizione Z del cluster gripper del Spent fuel rod.//
	stat = GetTagIdx("SFR_CL_POS", &tagidx );
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	stat = TagRead (&tagidx, &tagval);    
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	if(!((GtBit(tagval.qual.qf[0], Q1IMP) == 1) &&
		(GtBit(tagval.qual.qf[0], Q1OLDD) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1OFSC) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1HCFV) == 0)))
		return (RT_FAILED);
	else
		Info->PosZ[SPENT_FUEL_ROD].CLG_PosZ = tagval.value.val_float;
	
	//Prelevo lo stato dell'FA gripper latch del Spent fuel rod.//
	stat = GetTagIdx("SFR_FIX_OP", &tagidx );
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	stat = TagRead (&tagidx, &tagval);    
	if ( stat != R_NORMAL )
		return (RT_FAILED);
	if(!((GtBit(tagval.qual.qf[0], Q1IMP) == 1) &&
		(GtBit(tagval.qual.qf[0], Q1OLDD) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1OFSC) == 0) &&
		(GtBit(tagval.qual.qf[0], Q1HCFV) == 0)))
		return (RT_FAILED);
	else
		Info->PosZ[SPENT_FUEL_ROD].FAL_Open = (int)tagval.value.val_di;
	*/
	return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		Get_Mast_TV_Pos
**
** Description:		Get Main Mast TV Actual Position from TAGS in DB
**
** Output:			Angle		-	TV mast angle;
**
** Return:			RETSTAT		-	RT_NORMAL	
**								-	RT_FAILED	
*/

RETSTAT Get_Mast_TV_Pos(float *Angle){
	RETCOND		stat;
	TAGIDX		tagidx;
	TAGVALEX	tagval;

	*Angle = 0;

	stat = GetTagIdx(TWN_TV_ROT_POS, &tagidx);
	if ( stat != R_NORMAL )
		return (RT_FAILED);

	stat = TagRead (&tagidx, &tagval);    
	if ( stat != R_NORMAL )
		return (RT_FAILED);

	/*
	if ( !( ( GtBit ( tagval.qual.qf[0], Q1IMP  ) == 1 ) &&
			( GtBit ( tagval.qual.qf[0], Q1OLDD ) == 0 ) &&
			( GtBit ( tagval.qual.qf[0], Q1OFSC ) == 0 ) &&
			( GtBit ( tagval.qual.qf[0], Q1HCFV ) == 0 ) ) ){
		return (RT_FAILED);
	}
	*/
	if ( !( ( GtBit ( tagval.qual.qf[0], Q1IMP  ) == 1 ) &&
			( GtBit ( tagval.qual.qf[0], Q1OLDD ) == 0 ) &&
			( GtBit ( tagval.qual.qf[0], Q1OFSC ) == 0 ) &&
			( GtBit ( tagval.qual.qf[0], Q1HCFV ) == 0 ) ) ){
		*Angle = (float)(0);
	}	
	else{
		/*
		for( Tv_Angle = 0; Tv_Angle <= 315; Tv_Angle += 45){
			if( (tagval.value.val_float >= ((float)Tv_Angle - TOLL_MM_GR) )&&
				(tagval.value.val_float <= ((float)Tv_Angle + TOLL_MM_GR) ) ){
				*Angle = (float)Tv_Angle;
				break;
			}
		}
		*/
		float toll;
		GetMMRotToll(&toll);

		if(tagval.value.val_float < 0.0){
			if(tagval.value.val_float >= (float)(0.0 - toll))
				tagval.value.val_float = 0.0;
			else
				return (RT_FAILED);
		}
		if(tagval.value.val_float > 315.0){
			if(tagval.value.val_float <= (float)(315.0 + toll))
				tagval.value.val_float = 315.0;
			else
				return (RT_FAILED);
		}
		*Angle = tagval.value.val_float;
	}
	return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		Check_Mast_Mounted
**
** Description:		Check if a mast is mounted
**
** Input:			mastIdx		-	Idx of mast to check.
**
** Return:			RETSTAT		-	RT_NORMAL	
**								-	RT_FAILED	
*/
RETSTAT	CheckMastMounted(GENIDX mastIdx){
	/*
	RETCOND		stat;
	TAGIDX		tagidx;
	TAGVALEX	tagval;

	
	if((mastIdx < 0) && (mastIdx > SPENT_FUEL_ROD))
		return RT_FAILED;

	switch(mastIdx){
	case MAIN_MAST:
		stat = GetTagIdx( "A_M_ROD_MOUNT", &tagidx);
		if ( stat != R_NORMAL )
			return (RT_FAILED);

		stat = TagRead (&tagidx, &tagval);    
		if ( stat != R_NORMAL )
			return (RT_FAILED);

		if(tagval.value.val_di == 1)
			return RT_NORMAL;
		break;
	case NEW_FUEL_ROD:
		stat = GetTagIdx( "A_NFR_MOUNT", &tagidx);
		if ( stat != R_NORMAL )
			return (RT_FAILED);

		stat = TagRead (&tagidx, &tagval);    
		if ( stat != R_NORMAL )
			return (RT_FAILED);

		if(tagval.value.val_di == 1)
			return RT_NORMAL;
		break;
	
	case SPENT_FUEL_ROD:
		stat = GetTagIdx( "TV_ANGLE", &tagidx);
		if ( stat != R_NORMAL )
			return (RT_FAILED);

		stat = TagRead (&tagidx, &tagval);    
		if ( stat != R_NORMAL )
			return (RT_FAILED);

		if(tagval.value.val_di == 1)
			return RT_NORMAL;
		break;
	default:
		break;
	
	}
	*/
	return RT_FAILED;
}

/*------------------------------------------------------------------------------
** Function:		GetDynTVAngle
**
** Description:		Retrieve dynamic TV angle
**
** Output:			angle		-	TV angle
**
** Return:			RETSTAT		-	RT_NORMAL	
**								-	RT_FAILED	
*/
RETSTAT GetDynTVAngle(float *angle){
	RETCOND			retCnd;
	FILEIDX			fileDynp;
	dynamic_struc	dyn;

	fileDynp.num = F_DYNP;

	retCnd = FileRead(&fileDynp, 1, 1, &dyn, sizeof(dyn));
	if(retCnd != R_NORMAL)
		return(RT_FAILED);

	*angle = dyn.angle;
	return(RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		SetDynTVAngle
**
** Description:		Set dynamic TV angle
**
** Input:			angle		-	TV angle
**
** Return:			RETSTAT		-	RT_NORMAL	
**								-	RT_FAILED	
*/
RETSTAT SetDynTVAngle(float angle){
	RETCOND			retCnd;
	FILEIDX			fileDynp;
	dynamic_struc	dyn;

	fileDynp.num = F_DYNP;
	
	if(angle < 0)
		angle = 0.0;
	else if(angle > 315)
		angle = 315.0;
	
	retCnd = FileRead(&fileDynp, 1, 1, &dyn, sizeof(dyn));
	if(retCnd != R_NORMAL)
		return(RT_FAILED);

	dyn.angle = angle;
	
	retCnd = FileWrite(&fileDynp, 1, 1, &dyn, sizeof(dyn));
	if(retCnd != R_NORMAL)
		return(RT_FAILED);

	return(RT_NORMAL);
}



/*------------------------------------------------------------------------------
** Function:		InsertObjectEx
**
** Description:		Insert Object in Cell and Remove from Mast
**
** Input:			vx			- vit. coord. X of the cell
**					vy			- vit. coord. Y of the cell
**					ObjtMast	- Mast Situation
**					ObjectIdx	- Object Idx (present only for CASS CLUS, HECA)
**					ObjtIdx		- Object Type Idx
**
** Output:
**
** Return:			RETSTAT		- RT_NORMAL	
**								- RT_FAILED	
*/

RETSTAT InsertObjectEx(short int vx, short int vy, OBJTCELL	*ObjtCell, OBJTMM *ObjtMast, GENIDX ObjectIdx, GENIDX ObjtIdx){
	RETSTAT		stat;
	GENIDX		ObjObjtIdx;
	GENIDX		AppObjtIdx;
	int			ObjtGenCode;
	GENIDX		cellType;

	stat = GetCellType(vx, vy, &cellType);
	if(stat != RT_NORMAL)
		return(stat);

	if(ObjectIdx != 0){
		/* GET TYPE OF OBJECT */
		stat = GetObjObjtIdx ( ObjectIdx, &ObjObjtIdx );
		if(stat != RT_NORMAL)
			return ( RT_FAILED );
		
		/* CHECK THAT OBJECT TYPE AND TYPE OF OBJECT MATCH */
		if( ObjObjtIdx != ObjtIdx )
			return (RT_FAILED);

		/* GET GENERIC TYPE CODE OF OBJECT TO INSERT */
		stat = GetObjtGenericTypeCode  ( ObjObjtIdx, &ObjtGenCode );
		if ( stat != RT_NORMAL )
			return ( RT_FAILED );
    
		/* IF GENERIC TYPE OF OBJECT IS OF NAMED TYPE */
		if ( ObjtGenCode < N_MAX_NAMED_OBJ ){
			
			/* CHECK THAT EXIST A NAMED OBJECT OF THIS GENERIC TYPE */
			if ( ObjtMast->o_id[ObjtGenCode] != ObjectIdx )
				return (RT_FAILED);

			/* IF WE ARE INSERTING HECA, INSERT ALSO COVER, CASS AND CLUS (IF EXISTS) */
			if(ObjtGenCode == OBJT_N_HECA){
				if(ObjtCell->obj[OBJT_N_HECA] != 0)
					return(RT_FAILED);
			
				ObjtCell->o_id[OBJT_N_HECA] = ObjtMast->o_id[OBJT_N_HECA];
				GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_HECA], &AppObjtIdx );
				ObjtCell->obj [OBJT_N_HECA] = AppObjtIdx;
				ObjtMast->obj [OBJT_N_HECA] = (GENIDX)0;
				ObjtMast->o_id[OBJT_N_HECA] = (GENIDX)0;
				GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );
				if ( ObjtMast->o_id[OBJT_N_FA] != 0 ){
					ObjtCell->o_id[OBJT_N_FA] = ObjtMast->o_id[OBJT_N_FA];
					GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_FA], &AppObjtIdx );
					ObjtCell->obj [OBJT_N_FA] = AppObjtIdx;
					ObjtMast->obj [OBJT_N_FA] = (GENIDX)0;
					ObjtMast->o_id[OBJT_N_FA] = (GENIDX)0;
					GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );
				}
				if ( ObjtMast->obj[OBJT_N_COVER] != 0 ){
					ObjtCell->obj[OBJT_N_COVER] = ObjtMast->obj[OBJT_N_COVER];
					ObjtCell->o_id[OBJT_N_COVER] = ObjtMast->o_id[OBJT_N_COVER];
					GetObjectFromMastSituation ( ObjtMast, ObjtMast->obj[OBJT_N_COVER] );
					ObjtMast->obj[OBJT_N_COVER] = (GENIDX)0;
					ObjtMast->o_id[OBJT_N_COVER] = (GENIDX)0;
				}
				if ( ObjtMast->obj[OBJT_N_CLUS] != 0 ){
					ObjtCell->obj[OBJT_N_CLUS] = ObjtMast->obj[OBJT_N_CLUS];
					ObjtCell->o_id[OBJT_N_CLUS] = ObjtMast->o_id[OBJT_N_CLUS];
					GetObjectFromMastSituation ( ObjtMast, ObjtMast->obj[OBJT_N_CLUS] );
					ObjtMast->obj[OBJT_N_CLUS] = (GENIDX)0;
					ObjtMast->o_id[OBJT_N_CLUS] = (GENIDX)0;
				}
			}

			/* IF WE ARE INSERTING CASS, INSERT ALSO CLUS (IF EXIST) */
			if (ObjtGenCode == OBJT_N_FA){
				if((ObjtCell->obj[OBJT_N_FA] != 0) || (ObjtCell->obj[OBJT_N_CLUS_CAS] != 0) || (ObjtCell->obj[OBJT_N_CLUS_FRM] != 0))
					return(RT_FAILED);
				ObjtCell->o_id[OBJT_N_FA] = ObjtMast->o_id[OBJT_N_FA];
				GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_FA], &AppObjtIdx );
				ObjtCell->obj [OBJT_N_FA] = AppObjtIdx;
				ObjtMast->obj [OBJT_N_FA] = (GENIDX)0;
				ObjtMast->o_id[OBJT_N_FA] = (GENIDX)0;
				GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );

				if ( ObjtMast->o_id[OBJT_N_CLUS] != 0 ){
					ObjtCell->o_id[OBJT_N_CLUS] = ObjtMast->o_id[OBJT_N_CLUS];
					GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_CLUS], &AppObjtIdx );
					ObjtCell->obj [OBJT_N_CLUS] = AppObjtIdx;
					ObjtMast->obj [OBJT_N_CLUS] = (GENIDX)0;
					ObjtMast->o_id[OBJT_N_CLUS] = (GENIDX)0;
					GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );
				}
			}

			if (ObjtGenCode == OBJT_N_CLUS_FRM){
				if((ObjtCell->obj[OBJT_N_FA] != 0) || (ObjtCell->obj[OBJT_N_CLUS_CAS] != 0) || (ObjtCell->obj[OBJT_N_CLUS_FRM] != 0))
					return(RT_FAILED);
				ObjtCell->o_id[OBJT_N_CLUS_FRM] = ObjtMast->o_id[OBJT_N_CLUS_FRM];
				GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_CLUS_FRM], &AppObjtIdx );
				ObjtCell->obj [OBJT_N_CLUS_FRM] = AppObjtIdx;
				ObjtMast->obj [OBJT_N_CLUS_FRM] = (GENIDX)0;
				ObjtMast->o_id[OBJT_N_CLUS_FRM] = (GENIDX)0;
				GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );

				if ( ObjtMast->o_id[OBJT_N_CLUS] != 0 ){
					ObjtCell->o_id[OBJT_N_CLUS] = ObjtMast->o_id[OBJT_N_CLUS];
					GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_CLUS], &AppObjtIdx );
					ObjtCell->obj [OBJT_N_CLUS] = AppObjtIdx;
					ObjtMast->obj [OBJT_N_CLUS] = (GENIDX)0;
					ObjtMast->o_id[OBJT_N_CLUS] = (GENIDX)0;
					GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );
				}
			}

			/* IF WE ARE INSERTING CLUS */
			if ( ObjtGenCode == OBJT_N_CLUS ){
				if(ObjtCell->obj[OBJT_N_CLUS] != 0)
					return(RT_FAILED);

				if(cellType == CELL_FA){
					if((ObjtCell->obj[OBJT_N_FA] == 0) && (ObjtCell->obj[OBJT_N_CLUS_FRM] == 0))
						return(RT_FAILED);
				}
				else if(cellType == CELL_COVER_PLUG){
					if((ObjtCell->obj[OBJT_N_COVER] != 0) || (ObjtCell->obj[OBJT_N_FFDS_COV] != 0))
						return(RT_FAILED);
				}
				else
					return(RT_FAILED);

				ObjtCell->o_id[OBJT_N_CLUS] = ObjtMast->o_id[OBJT_N_CLUS];
				GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_CLUS], &AppObjtIdx );
				ObjtCell->obj [OBJT_N_CLUS] = AppObjtIdx;
				ObjtMast->obj [OBJT_N_CLUS] = (GENIDX)0;
				ObjtMast->o_id[OBJT_N_CLUS] = (GENIDX)0;
				GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );
			}

			if ( ObjtGenCode == OBJT_N_COVER){
				if(ObjtCell->obj[OBJT_N_COVER] != 0)
					return(RT_FAILED);
				if((cellType != CELL_HECA) && (cellType != CELL_COVER_PLUG) && (cellType != CELL_COVER_PLUG_EX))
					return(RT_FAILED);
				if((cellType == CELL_HECA) && (ObjtCell->obj[OBJT_N_HECA] == 0))
					return(RT_FAILED);
				ObjtCell->o_id[OBJT_N_COVER] = ObjtMast->o_id[OBJT_N_COVER];
				GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_COVER], &AppObjtIdx );
				ObjtCell->obj [OBJT_N_COVER] = AppObjtIdx;
				ObjtMast->obj [OBJT_N_COVER] = (GENIDX)0;
				ObjtMast->o_id[OBJT_N_COVER] = (GENIDX)0;
				GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );
			}

			if(ObjtGenCode == OBJT_N_FFDS_COV){
				if((ObjtCell->obj[OBJT_N_FFDS_COV] != 0) || (ObjtCell->obj[OBJT_N_COVER] != 0))
					return(RT_FAILED);
				ObjtCell->o_id[OBJT_N_FFDS_COV] = ObjtMast->o_id[OBJT_N_FFDS_COV];
				GetObjObjtIdx ( ObjtMast->o_id[OBJT_N_FFDS_COV], &AppObjtIdx );
				ObjtCell->obj [OBJT_N_FFDS_COV] = AppObjtIdx;
				ObjtMast->obj [OBJT_N_FFDS_COV] = (GENIDX)0;
				ObjtMast->o_id[OBJT_N_FFDS_COV] = (GENIDX)0;
				GetObjectFromMastSituation ( ObjtMast, AppObjtIdx );
			}
		}
		else{
			/* GET GENERIC TYPE CODE OF OBJECT TO INSERT */
			stat = GetObjtGenericTypeCode  ( ObjtIdx, &ObjtGenCode );
			if ( stat != RT_NORMAL )
				return ( stat );

			/* CHECK THAT EXIST THIS GENERIC OBJECT TYPE ON MAST */
			if(ObjtMast->obj[ObjtGenCode] != ObjtIdx)
				return(RT_FAILED);
		
			/* INSERT OBJECT TYPE IN CELL */
			ObjtMast->obj[ObjtGenCode] = (GENIDX)0;
			GetObjectFromMastSituation ( ObjtMast, ObjtIdx );
			ObjtCell->obj[ObjtGenCode] = ObjtIdx;
		}
	}

	/* ** ** *** * ***** ****** */
	/* IF IS NOT A NAMED OBJECT */
	/* ** ** *** * ***** ****** */
	else{
		/* *** ******* **** **** ** ****** ** ****** */
		/* GET GENERIC TYPE CODE OF OBJECT TO INSERT */
		/* *** ******* **** **** ** ****** ** ****** */
		stat = GetObjtGenericTypeCode  ( ObjtIdx, &ObjtGenCode );
		if ( stat != RT_NORMAL )
			return ( stat );

		if(ObjtMast->obj[ObjtGenCode] != ObjtIdx)
			return(RT_FAILED);
	
		if(ObjtGenCode == OBJT_N_CLUS_CAS){
			if(cellType != CELL_COVER_PLUG_EX)
				return(RT_FAILED);
			if(ObjtMast->obj[OBJT_N_CLUS] != 0)
				return(RT_FAILED);
			else if((ObjtCell->obj[OBJT_N_FFDS_COV] != 0) || (ObjtCell->obj[OBJT_N_COVER] != 0))
				return(RT_FAILED);
		}
		else if(ObjtGenCode == OBJT_N_FFDS_COV){
			if((cellType != CELL_COVER_PLUG_EX) && (cellType != CELL_COVER_PLUG) && (cellType != CELL_FFDS_CASK))
				return(RT_FAILED);
			else if((ObjtCell->obj[OBJT_N_FFDS_COV] != 0) || (ObjtCell->obj[OBJT_N_COVER] != 0))
				return(RT_FAILED);
		}

		ObjtMast->obj[ObjtGenCode] = (GENIDX)0;
		GetObjectFromMastSituation ( ObjtMast, ObjtIdx );
		ObjtCell->obj[ObjtGenCode] = ObjtIdx;
	}


	/* *** *** **** ** **** */
	/* PUT NEW DATE IN CELL */
	/* *** *** **** ** **** */
	GetApmsTimeEx(&ObjtCell->LastDate);
	return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		GetDynCurrentMast
**
** Description:		Get Current Mast from Dynamic Parameters
**
** Input:
**
** Output:			Current_mm	- Current Mast Idx
**
** Return:			RETSTAT		- RT_NORMAL	If Ok
**								- RT_FAILED	If Error reading File
*/

RETSTAT GetDynCurrentMast		( GENIDX *Current_mm )
{
  RETCOND		stat;
  dynamic_struc	dyn;
  FILEIDX		file;

  file.num = F_DYNP;

  *Current_mm = (GENIDX)0;

  stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn) );
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  *Current_mm = dyn.mastType;

  return (RT_NORMAL);
}

/*------------------------------------------------------------------------------
** Function:		SetDynCurrentMast
**
** Description:		Set Current Mast in Dynamic Parameters
**
** Input:			Current_mm	- Current Mast Idx
**
** Output:
**
** Return:			RETSTAT		- RT_NORMAL	If Ok
**								- RT_FAILED	If Error writing File
*/

RETSTAT SetDynCurrentMast	(GENIDX Current_mm)
{
  RETCOND		stat;
  dynamic_struc	dyn;
  FILEIDX		file;

  file.num = F_DYNP;

  stat = FileRead( &file, 1, 1, &dyn, sizeof(dyn) );
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  dyn.mastType = Current_mm;

  stat = FileWrite( &file, 1, 1, &dyn, sizeof(dyn));
  if ( stat != R_NORMAL )
    return (RT_FAILED);

  return (RT_NORMAL);
}